package com.hurui.xiaoming.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.bean.Li_FinanceResult;
import com.hurui.xiaoming.bean.Li_SureSendMoney;
import com.hurui.xiaoming.http.ApiClient;
import com.hurui.xiaoming.http.ApiSevice;
import com.hurui.xiaoming.utils.DateUtil;
import com.hurui.xiaoming.utils.DefultData;
import com.hurui.xiaoming.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hurui on 2017/7/21.
 */

public class FinanceAdapter extends RecyclerView.Adapter<FinanceAdapter.FinanceHolder> {

    private Context context ;
    private List<Li_FinanceResult.DataBean> lists ;
    // 得到剪贴板管理器
    ClipboardManager cmb;
    public ApiSevice apiSevice = ApiClient.getModel().getService();
    Map map = new HashMap();
    public FinanceAdapter(Context context ){
        this.context = context ;
        cmb = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
       lists=new ArrayList<>();
    }

    public void notifyData(List<Li_FinanceResult.DataBean> list) {
            lists = list;
        notifyDataSetChanged();
    }
    @Override
    public FinanceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.finance_item , parent , false);
        return new FinanceHolder(view);
    }

    @Override
    public void onBindViewHolder(FinanceHolder holder, int position) {
        final Li_FinanceResult.DataBean dataBean = lists.get(position);


        switch (dataBean.getOi_state()) {
            case "1":
                //借款中
                holder.oi_num.setText("订单编号:"+dataBean.getOi_num());
                holder.order_info_tv.setText("客户【" + dataBean.getUi_name() + "】借款"+dataBean.getOi_jkprice()+"元，分" + dataBean.getOi_jkloans()+ "期");
                holder.order_status_tv.setText("未打款");
                holder.order_moeny.setText(dataBean.getOi_jkprice());
                if(dataBean.getUi_alipay() != null){
                    holder.zhifubao_tv.setText("支付宝账号（"+dataBean.getUi_alipay() + ")");
                }
                if (dataBean.getOi_dkaddtime() != null)
                    holder.order_time_tv.setText(DateUtil.changeTimeMiao(dataBean.getOi_dkaddtime()));
                holder.send_btn.setText("确认打款");
                break;
            case "2":
                holder.oi_num.setText("订单编号:"+dataBean.getOi_num());
                holder.order_info_tv.setText("客户【" + dataBean.getUi_name() + "】借款"+dataBean.getOi_jkprice()+"元，分" + dataBean.getOi_jkloans()+ "期");
                holder.order_status_tv.setText("已打款");
                holder.order_moeny.setText(dataBean.getOi_jkprice());
                if(dataBean.getUi_alipay() != null){
                    holder.zhifubao_tv.setText("支付宝账号（"+dataBean.getUi_alipay() + ")");
                }
                if (dataBean.getOi_dkaddtime() != null)
                    holder.order_time_tv.setText(TimeUtils.getFinancetime(dataBean.getOi_dkaddtime()));
                holder.copy_btn.setVisibility(View.GONE);
                holder.send_btn.setVisibility(View.GONE);
                break;
            case "4":
//                holder.order_info_tv.setText("客户【" + "张三" + "】借款"+dataBean.getOi_jkprice()+"元，分" + dataBean.getOi_jkloans()+ "期");
//                holder.order_status_tv.setText("已逾期");
//                holder.order_moeny.setText("您已逾期");
//                if (dataBean.getHktime() != null)
//                    holder.order_time_tv.setText(TimeUtils.getFinancetime(dataBean.getHktime()));
//                holder.send_btn.setText("立即打款");
                break;
            case "5":
//                holder.order_info_tv.setText("借款" + dataBean.getOi_jkprice() + "元，分" + dataBean.getOi_jkloans() + "期");
//                holder.order_status_tv.setText("已还款");
//                holder.order_moeny.setText(dataBean.getMyyhprice());
//                if (dataBean.getHktime() != null)
//                    holder.order_time_tv.setText(TimeUtils.getFinancetime(dataBean.getHktime()));
//                holder.send_btn.setVisibility(View.GONE);

                break;

        }
        holder.copy_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(null == dataBean.getUi_alipay() || "" == dataBean.getUi_alipay()){
                    Toast.makeText(context,"缺少用户支付宝账号",Toast.LENGTH_SHORT).show();
                }else {
                    cmb.setText(dataBean.getUi_alipay());
                    Toast.makeText(context,"已复制到剪切板",Toast.LENGTH_SHORT).show();
                }

            }
        });

        holder.send_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                map.clear();
                map.put("oi_id",dataBean.getOi_id());
                sureSendMoney();

            }
        });
    }
    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class FinanceHolder extends RecyclerView.ViewHolder{

        private final TextView oi_num,order_status_tv,order_info_tv,zhifubao_tv,order_moeny,
                order_time_tv,copy_btn,send_btn;

        public FinanceHolder(View itemView) {
            super(itemView);
            oi_num = ((TextView) itemView.findViewById(R.id.dingdan_code));
            order_status_tv = ((TextView) itemView.findViewById(R.id.order_status_tv));
            order_info_tv = ((TextView) itemView.findViewById(R.id.order_info_tv));
            order_moeny = ((TextView) itemView.findViewById(R.id.order_moeny));
            zhifubao_tv = ((TextView) itemView.findViewById(R.id.zhifubao_tv));
            order_time_tv = ((TextView) itemView.findViewById(R.id.order_time_tv));
            copy_btn = ((TextView) itemView.findViewById(R.id.copy_btn));
            send_btn = ((TextView) itemView.findViewById(R.id.send_btn));
        }
    }

    public void sureSendMoney(){
        Call<Li_SureSendMoney> call = apiSevice.onHttpPostSureMoney(DefultData.sureSendMonet,map);
        call.enqueue(new Callback<Li_SureSendMoney>() {
            @Override
            public void onResponse(Call<Li_SureSendMoney> call, Response<Li_SureSendMoney> response) {
                if(response.isSuccessful()){
                    Li_SureSendMoney li_sureSendMoney = response.body();
                    if("sucess".equals(li_sureSendMoney.getStatus())){
                        updateView.updateView();
                        Toast.makeText(context,"已打款",Toast.LENGTH_SHORT).show();
                    }else{

                    }
                }else{

                }
            }

            @Override
            public void onFailure(Call<Li_SureSendMoney> call, Throwable t) {

            }
        });
    }

    public interface UpdateView{
        void updateView();
    }

    private UpdateView updateView;     //字符串

    //定义接口入住对象
    public void setUpdateView(UpdateView updateView){
        this.updateView = updateView;
    }
}
